$(function () {
    nav_current();
    download_nav();
    help_nav();
    login_dialog();
    cp_table();
    chmb_fun();
    img_update();
    dialog();
    list_dialog();
    tabs();
    zdy_show();
    notice_list();
    del_list();
});

function nav_current() {
    var header_bottom_content = $('.header_bottom_content');
    header_bottom_content.find('a').each(function () {
        var $nav_href = $(this).attr('href');
        var local_attr = $nav_href.substr($nav_href.lastIndexOf('/') + 1);
        //正则删除包括问号以后的所有代码
        var href_attr = window.location.href.substr(window.location.href.lastIndexOf('/') + 1).replace(/\?.+/, "");
        if (local_attr.toLocaleLowerCase() == href_attr.toLocaleLowerCase()) {
            $(this).attr('class', 'current');
        }
    });
}

function download_nav() {
    var _showTab = 0;
    $('.tabs_box').each(function () {
        var $tab = $(this);
        var $defaultLi = $('ul.tabs_ul li', $tab).eq(_showTab).addClass('active');
        $($defaultLi.find('a').attr('href')).siblings().hide();
        $('ul.tabs_ul li', $tab).click(function () {
            var $this = $(this),
                _clickTab = $this.find('a').attr('href');
            $this.addClass('active').siblings('.active').removeClass('active');
            $(_clickTab).stop(false, true).fadeIn().siblings().hide();
            return false;
        }).find('a').focus(function () {
            this.blur();
        });
    });
}

function help_nav() {
    $(".subNav").click(function () {
        $(this).toggleClass("currentDd").siblings(".subNav").removeClass("currentDd");
        $(this).toggleClass("currentDt").siblings(".subNav").removeClass("currentDt");
        $(this).next(".navContent").slideToggle(300).siblings(".navContent").slideUp(500);
    })
}

function login_dialog() {
    var login_dialog_bg = $('.login_dialog_bg');
    var login_dialog = $('.login_dialog');
    $('#ljsq').on('click', function () {
        login_dialog_bg.show();
        login_dialog.show();
    });

    var close_btn = $('.login_dialog button');
    close_btn.on('click', function () {
        login_dialog_bg.hide();
        login_dialog.hide();
    });
}

function cp_table() {
    var cp_table_close = $('.cp_table_close');
    var table_tips = $('.table_tips');
    cp_table_close.on('click', function () {
        table_tips.hide();
    });
}

function chmb_fun() {
    var login_dialog_bg = $('.login_dialog_bg');
    var cp_table_dialog = $('.cp_table_dialog');

    var chmb = $('.chmb');
    chmb.on('click', function () {
        login_dialog_bg.show();
        cp_table_dialog.show();
    });

    var qx = $('.cp_table_dialog .qx');
    qx.on('click', function () {
        login_dialog_bg.hide();
        cp_table_dialog.hide();
    });
}

// 上传icon
function img_update() {
    $('#file_01').change(function () {
        setImagePreview('localImag_01', 'preview_01', 'file_01');
        $('#img_01').val(function () {
            return $('#file_01').val();
        });
    });

    $('#file_02').change(function () {
        setImagePreview('localImag_02', 'preview_02', 'file_02');
        $('#img_02').val(function () {
            return $('#file_02').val();
        });
    });
}

// dialog
function dialog() {
    var dialog_close = $('.dialog_close');
    var dialog_bg = $('.dialog_bg');
    var dialog = $('.dialog');
    dialog_close.on('click', function () {
        dialog_bg.hide();
        dialog.hide();
        location.reload();
    });

    var tjazqd_btn = $('#tjazqd_btn');
    var tjqd_dialog = $('.tjqd_dialog');
    tjazqd_btn.on('click', function () {
        dialog_bg.show();
        tjqd_dialog.show();
    });

    var qdb_btn = $('#qdb_btn');
    var qdb_dialog = $('.qdb_dialog');
    qdb_btn.on('click', function () {
        dialog_bg.show();
        qdb_dialog.show();
    });
}

// list_dialog
function list_dialog() {
    var bm = $('#bm');
    var appid = $('#appid');
    var appkey = $('#appkey');
    var appsecret = $('#appsecret');
    var blcspz = $('#blcspz');
    var pz_btn = $('.pz_btn');

    bm.on('blur', function () {
        if (bm.val() == '') {
            $(this).next('.bnwk').show();
        } else {
            $(this).next('.bnwk').hide();
        }
    });
    appid.on('blur', function () {
        if (appid.val() == '') {
            $(this).next('.bnwk').show();
        } else {
            $(this).next('.bnwk').hide();
        }
    });
    appkey.on('blur', function () {
        if (appkey.val() == '') {
            $(this).next('.bnwk').show();
        } else {
            $(this).next('.bnwk').hide();
        }
    });
    appsecret.on('blur', function () {
        if (appsecret.val() == '') {
            $(this).next('.bnwk').show();
        } else {
            $(this).next('.bnwk').hide();
        }
    });
}

// 清空文本
function clearErrorLabel() {
    $('.qdbm_cspz_l').find('input[type=text]').val('');
    $('.zdysp').find('input[type=text]').val('');
    $('.kfcsxt_content').find('input[type=text]').val('');
}

// 渠道包名,参数配置
function channelParam(channelId) {
    clearErrorLabel();

    var dialog_bg = $('.dialog_bg');
    var qdbm_cspz = $('.qdbm_cspz');
    dialog_bg.show();
    qdbm_cspz.show();

    var bm = $('#bm');
    var appid = $('#appid');
    var appkey = $('#appkey');
    var appsecret = $('#appsecret');

    var qdbm_name = $('.qdbm_name');

    // 回显
    $.ajax({
        type: 'get',
        url: '/Public/ymn/js/cspz.json',
        dataType: 'json'
    }).done(function (result) {
        qdbm_name.text(function () {
            return result[channelId][0];
        });
        bm.val(function () {
            return result[channelId][1];
        });
        appid.val(function () {
            return result[channelId][2];
        });
        appkey.val(function () {
            return result[channelId][3];
        });
        appsecret.val(function () {
            return result[channelId][4];
        });
    });

    // 提交
    $('#blcspz').on('click', function () {
        var bm_val = bm.val();
        var appid_val = appid.val();
        var appkey_val = appkey.val();
        var appsecret_val = appsecret.val();
        $.ajax({
            type: 'post',
            data: {
                channelId: channelId,
                bm_val: bm_val,
                appid_val: appid_val,
                appkey_val: appkey_val,
                appsecret_val: appsecret_val
            },
            url: '/Public/ymn/js/cspz.json',
            dataType: 'json'
        }).done(function (result) {
            location.reload();
        });
    });
}

// 回调地址配置
function hddzpz(channelId) {

    var dialog_bg = $('.dialog_bg');
    var hddzpz_dialog = $('.hddzpz_dialog');
    dialog_bg.show();
    hddzpz_dialog.show();

    var qdbm_name = $('.qdbm_name');
    var hddz_input = $('#hddz_input');
    var hddz_span = $('#hddz_span');

    // 回显
    $.ajax({
        type: 'get',
        url: '/Public/ymn/js/hddzpz.json',
        dataType: 'json'
    }).done(function (result) {
        qdbm_name.text(function () {
            return result[channelId][0];
        });
        hddz_input.val(function () {
            return result[channelId][1];
        });
        hddz_span.text(function () {
            return result[channelId][2];
        });
    });

    $('#wywc_btn').on('click', function () {
        $.ajax({
            type: 'get',
            data: {
                channelId: channelId,
                form_val: $('#hddzpz_form').serialize()
            },
            url: 'hddzpz.json',
            dataType: 'json',
            success: function (msg) {
                location.reload();
            }
        });
    });
}

function tabs() {
    var _showTab = 0;
    $('.dialog_tabs_box').each(function () {
        var $tab = $(this);
        var $defaultLi = $('ul.dialog_tabs_ul li', $tab).eq(_showTab).addClass('active');
        $($defaultLi.find('a').attr('href')).siblings().hide();
        $('ul.dialog_tabs_ul li', $tab).click(function () {
            var $this = $(this),
                _clickTab = $this.find('a').attr('href');
            $this.addClass('active').siblings('.active').removeClass('active');
            $(_clickTab).stop(false, true).fadeIn().siblings().hide();
            return false;
        }).find('a').focus(function () {
            this.blur();
        });
    });
}

function sp(channelId) {
    clearErrorLabel();

    var dialog_bg = $('.dialog_bg');
    var sp_dialog = $('.sp_dialog');

    dialog_bg.show();
    sp_dialog.show();

    var sp_name = $('.sp_name');

    // 回显标题
    $.ajax({
        type: 'get',
        url: '/Public/ymn/js/pro_name.json',
        dataType: 'json'
    }).done(function (result) {
        sp_name.text(function () {
            return result[channelId][0];
        });
    });

    // 提交
    $('#sp_btn').on('click', function () {
        $.ajax({
            type: 'get',
            data: {
                channelId: channelId,
                form_val: $('#tab_radio').serialize()
            },
            url: 'sp.json',
            dataType: 'json',
            success: function (msg) {
                location.reload();
            }
        });
    });
}

function kf(channelId) {
    clearErrorLabel();

    var dialog_bg = $('.dialog_bg');
    var kfcsxt_dialog = $('.kfcsxt_dialog');

    dialog_bg.show();
    kfcsxt_dialog.show();

    // 删除
    function del_col() {
        $('.sc_btn').on('click', function () {
            $(this).parent().remove();
        });
    }

    // 生成随机数
    function getRandom(min, max) {
        var r = Math.random() * (max - min);
        var re = Math.round(r + min);
        re = Math.max(Math.min(re, max), min);
        return re;
    }

    // 直接插入行列一
    var am_add = $('#am_add');
    var r_id = getRandom(0, 100000000);
    var name_id = 'name_' + r_id;
    var value_id = 'value_' + r_id;
    var am_html = '<li><label for="' + name_id + '">name:</label><input id="' + name_id + '" type="text" name="' + name_id + '" /><label for="' + value_id + '">value:</label><input id="' + value_id + '" type="text" name="' + value_id + '" /></li>';
    am_add.append(am_html);
    // 添加行列一
    $('#am_btn').on('click', function () {
        var r_id = getRandom(0, 100000000);
        var name_id = 'name_' + r_id;
        var value_id = 'value_' + r_id;
        var am_html = '<li><label for="' + name_id + '">name:</label><input id="' + name_id + '" type="text" name="' + name_id + '" /><label for="' + value_id + '">value:</label><input id="' + value_id + '" type="text" name="' + value_id + '" /><span class="sc_btn">删除</span></li>';
        am_add.append(am_html);
        del_col();
    });

    // 直接插入行列二
    var th_add = $('#th_add');
    var s_id = getRandom(0, 100000000);
    var lj_id = 'lj_' + s_id;
    var wj_id = 'wj_' + s_id;
    var th_html = '<li><label for="' + lj_id + '">路径:</label><input id="' + lj_id + '" type="text" placeholder="例如:/res/drawable/xxxxxx.png" name="' + lj_id + '" /><label for="' + wj_id + '">文件:</label><input id="' + wj_id + '" type="text" name="' + wj_id + '" /></li>';
    th_add.append(th_html);
    // 添加行列二
    $('#thzy_btn').on('click', function () {
        var s_id = getRandom(0, 100000000);
        var lj_id = 'lj_' + s_id;
        var wj_id = 'wj_' + s_id;
        var th_html = '<li><label for="' + lj_id + '">路径:</label><input id="' + lj_id + '" type="text" placeholder="例如:/res/drawable/xxxxxx.png" name="' + lj_id + '" /><label for="' + wj_id + '">文件:</label><input id="' + wj_id + '" type="text" name="' + wj_id + '" /><span class="sc_btn">删除</span></li>';
        th_add.append(th_html);
        del_col();
    });

    var sp_name = $('.sp_name');
    // 回显标题
    $.ajax({
        type: 'get',
        url: '/Public/ymn/js/pro_name.json',
        dataType: 'json'
    }).done(function (result) {
        sp_name.text(function () {
            return result[channelId][0];
        });
    });

    // 提交
    $('#kfcsxt_btn').on('click', function () {
        $.ajax({
            type: 'get',
            data: {
                channelId: channelId,
                form_val: $('#kfcsxt_form').serialize()
            },
            url: 'kfcsxt.json',
            dataType: 'json',
            success: function (msg) {
                location.reload();
            }
        });
    });
}

// 删除列表
function del_list() {
    var self;
    var channelId;
// show dialog
    $(document).on('click', '.del_class', function () {
        var dialog_bg = $('.dialog_bg');
        var del_dialog = $('.del_dialog');
        dialog_bg.show();
        del_dialog.show();

        self = $(this);
        channelId = self.data('id');
    });
//  hide dialog
    $('.close_box').on('click', function () {
        var dialog_bg = $('.dialog_bg');
        var del_dialog = $('.del_dialog');
        dialog_bg.hide();
        del_dialog.hide();
    });
// dialog提交代码
    $('#del_qd_btn').on('click', function () {
        $.ajax({
            type: 'get',
            data: {
                channelId: channelId
            },
            url: 'del_list.json',
            dataType: 'json',
            success: function () {
                self.parents('tr').remove();
            }
        });
        console.log(channelId);
    });
}


// 修改列表
function edit_list(channelId) {
    clearErrorLabel();

    var dialog_bg = $('.dialog_bg');
    var edit_list = $('.edit_list');
    dialog_bg.show();
    edit_list.show();

    var bjqdxx_cjsj = $('#bjqdxx_cjsj');
    var bjqdxx_qdbm = $('#bjqdxx_qdbm');
    // 回显标题
    $.ajax({
        type: 'get',
        url: '/Public/ymn/js/bjqdxx.json',
        dataType: 'json'
    }).done(function (result) {
        bjqdxx_cjsj.text(function () {
            return result[channelId][0];
        });
        bjqdxx_qdbm.text(function () {
            return result[channelId][1];
        });
    });

    // 提交
    $('#bjqdxx_btn').on('click', function () {
        $.ajax({
            type: 'get',
            data: {
                channelId: channelId,
                bjqdxx_qdm: $('#bjqdxx_qdm').val()
            },
            url: 'bjqdxx.json',
            dataType: 'json',
            success: function (msg) {
                location.reload();
            }
        });
    });
}

function zdy_show() {
    var zdysc_input = $('.zdysc_input');
    var gdsc_input = $('.gdsc_input');
    var icsd = $('.icsd');
    zdysc_input.on('click', function () {
        icsd.show();
    });
    gdsc_input.on('click', function () {
        icsd.hide();
    });
}

//  通知删除功能
function notice_list() {
    $('.notice_bg').find('li').on('click', function () {
        $(this).find('.nc_text').toggle();
    });
    $('.del_icon').on('click', function () {
        var list_id = $(this).parent().attr('id');
        $(this).parent().remove();
        $.ajax({
            type: 'get',
            url: 'some.php',
            data: 'list_id=' + list_id,
            success: function () {

            }
        });
    });
}